package com.shop.user.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.user.api.mapper.UserMapper;
import com.shop.user.model.User;
import com.shop.user.model.search.UserSearchBean;
import com.shop.user.service.UserService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    @Override
    public Page<User> findAll(Page<User> page, UserSearchBean bsb) {
        return userMapper.findAll(page, bsb);
    }


    @Override
    public boolean save(User user) {
        return userMapper.insert(user) == 1;
    }

    @Override
    public int update(User user) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(User.class)
                .eq(User::getUsername, user.getUsername())
                .ne(User::getId, user.getId());
        if (userMapper.selectCount(queryWrapper) > 0) {
            return 2;
        }
        user.setUpdatedTime(LocalDateTime.now());
        return userMapper.updateById( user);
    }
    @Override
    public int add(User user) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(User.class)
                .eq(User::getUsername, user.getUsername())
                .ne(User::getId, user.getId());
        if (userMapper.selectCount(queryWrapper) > 0) {
            return 2;
        }
        user.setCreatedTime(LocalDateTime.now());
        System.out.println( user.getCreatedTime());
        return userMapper.insert( user);
    }

    @Override
    public User findByInput(String username) {
        return userMapper.findByInput(username);
    }

    @Override
    public int assignUser(User user) {
        return userMapper.assignUser(user);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return userMapper.deleteByIds(ids);
    }
    @Override
    public int deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public boolean updateEnabled(User user) {
        return userMapper.updateEnabled( user);
    }
    @Override
    public boolean updateLocked(User user) {
        return userMapper.updateLocked( user);
    }

}
